/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary.report;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import entity.*;
import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

/**
 *
 * @author JAVIER
 */
public class Bill implements JRDataSource {
    
        public Bill(ServiceRequest ser){
        service = ser;
    }

    private ServiceRequest service = new ServiceRequest();
    private int sampleactually = -1;

    public Object getFieldValue(JRField jrf) throws JRException
    {
        Object valor = null;
        Sample sample = null;
        Client client = null;
        Billing bill = null;
        DatabaseTest databaseTest = null;

        if ("cliente".equals(jrf.getName()))
        {
            client = (Client)service.getClient();
            valor = client.getName();
        }
        else if ("telefono".equals(jrf.getName()))
        {
            client = (Client)service.getClient();
            if (client.getPhoneNumbers().size() > 0) {
                valor = client.getPhoneNumbers().get(0);
            } else {
                valor = "";
            }
        }
        else if ("fecha".equals(jrf.getName()))
        {
            bill = (Billing) service.getBilling();
            //valor = (String)bill.getBillingDate();
        }
        else if ("direnvio".equals(jrf.getName()))
        {
            client = (Client)service.getClient();
            if (client.getAddresses().size() > 0) {
                valor = client.getAddresses().get(0);
            } else {
                valor = "";
            }
        }      
        else if ("ciudad".equals(jrf.getName()))
        {
            valor = "Bogotá D.C.";
        }
        else if ("cantidad".equals(jrf.getName()))
        {
            sample = (Sample) service.getSamples().get(sampleactually);
            valor = sample.getQuantity();
        }
        else if ("descripcion".equals(jrf.getName()))
        {
            sample = (Sample) service.getSamples().get(sampleactually);
            valor = sample.getName();
        }
        else if ("numero".equals(jrf.getName()))
        {
            bill = (Billing)service.getBilling();
            valor = bill.getBillNumber();
        }
        else if ("deposito".equals(jrf.getName()))
        {
            bill = (Billing)service.getBilling();
            valor = bill.getDeposit();
        }
        else if ("prendiente".equals(jrf.getName()))
        {
            bill = (Billing)service.getBilling();
            valor = bill.getDeposit();
        }
        else if ("observaciones".equals(jrf.getName()))
        {
            sample = (Sample)service.getSamples().get(sampleactually);
            valor = "";
        }
        else if ("analisis".equals(jrf.getName()))
        {
            sample = (Sample)service.getSamples().get(sampleactually);

            valor = getSampleDS(sample);
        }

        return valor;
    }

    public JRDataSource getTestDS(Sample ped)   
    {   
        return new JRBeanCollectionDataSource(ped.getTests());   
    } 
    
    public JRDataSource getSampleDS(Sample ped)   
    {   
        SampleDataSource sds = new SampleDataSource(ped);
        return sds;
    } 
    
    public boolean next() throws JRException
    {
        return ++sampleactually < service.getSamples().size();
    }
    
    public void Print() throws JRException{
        
        Bill datasource = this;
        
        JasperReport reporte = (JasperReport) JRLoader.loadObject("FacturaAlpha1.jasper");  
        JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, null,datasource );  

        JRExporter exporter = new JRPdfExporter();  
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
        exporter.setParameter(JRExporterParameter.OUTPUT_FILE, new java.io.File("FacturaPDF.pdf")); 
        exporter.exportReport();
    
    }
    
    
}
